package com.xingzhe.pengji.security.controller.system;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xingzhe.pengji.framework.controller.PaginationController;
import com.xingzhe.pengji.security.domain.Resource;
import com.xingzhe.pengji.security.services.ResourceService;

/**
 * @Title ： 三江学院 毕业设计
 * @Description ：
 * @author ：LuFengLiang
 * @Company: IT行者工作室
 * @Copyright: Copyright (c) 2012-2-24
 * @version ： 1.0
 */
@Controller
@RequestMapping("/resource")
public class ResourceController extends PaginationController
{

    private Resource resource;
    private List<Resource> resourcesList;
    private ModelAndView modelAndView;
    @Autowired
    private ResourceService<Resource> resourceService;
    private String operatorManString = "Admin";
    private final String GROUP_INDEX = "redirect:/resource/index.html";

    private final static Logger logger = LoggerFactory.getLogger(ResourceController.class);

    @Override
    protected ModelAndView executePaginationController(HttpServletRequest request, HttpServletResponse response,
            int pageNumber, int pageSize) throws Exception
    {

        resourcesList = resourceService.queryForPageResourceList(null, "id", pageSize, pageNumber, false);
        modelAndView = new ModelAndView();
        modelAndView.addObject("resourcesList", resourcesList);
        modelAndView.setViewName("/jsp/resource/queryPortalResource");

        return modelAndView;
    }

    @Override
    protected int getItemCount(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        //logger.debug(resource.toString());
        return resourceService.getItemCount(resource);
    }

    @RequestMapping(method = RequestMethod.GET, value = "/add.html")
    public String addResource()
    {
        return "/jsp/resource/addResource";
    }

    /**
     * 添加资源选项
     * @param resource
     * @return
     */
    @RequestMapping(value = "/save.html")
    public String saveesource(@ModelAttribute("resource")
    Resource resource)
    {
        resource.setCreateTime(new Date());
        resource.setCreator(operatorManString);
        resource.setUpdateTime(new Date());
        resource.setUpdateMan(operatorManString);
        logger.debug(resource.toString());
        resourceService.saveResource(resource);
        return GROUP_INDEX;
    }

    /**
     * 更新资源选项
     * @param resource
     * @return
     */
    @RequestMapping(value = "/saveOrUpdate.html")
    public String saveOrUpdateResource(@ModelAttribute("resource")
    Resource resource)
    {
        resource.setUpdateTime(new Date());
        resource.setUpdateMan(operatorManString);
        resourceService.updateResource(resource);
        return GROUP_INDEX;
    }

    @RequestMapping(value = "/update.html")
    public ModelAndView updateResource(@RequestParam("resourceIds")
    String resourceIds)
    {

        String[] stringargs = resourceIds.split(",");

        resource = new Resource();
        resource.setId(Integer.valueOf(stringargs[0]));
        resource = resourceService.getResourceById(resource);
        modelAndView = new ModelAndView();
        modelAndView.addObject("resource", resource);
        modelAndView.setViewName("/jsp/resource/updateResource");

        return modelAndView;
    }

    @RequestMapping(value = "/delete.html")
    public String delResource(@RequestParam("resourceIds")
    String resourceIds)
    {

        String[] argsString = resourceIds.split(",");
        resource = new Resource();
        for ( String string : argsString )
        {
            resource.setId(Integer.valueOf(string));
            resource.setUpdateTime(new Date());
            resourceService.delResource(resource);
        }
        return GROUP_INDEX;
    }

    /**
     * 判断用户名是否存在
     * @param   name
     * @return  返回 json 格式的true或者false
     */
    @ResponseBody
    @RequestMapping(value = "/validateName.html")
    public Object validateAccount(@RequestParam("name")
    String name)
    {
        resource = new Resource();
        resource.setName(name);
        resource = resourceService.getResourceByName(resource);
        if ( null == resource )
        {
            return true;
        }
        return false;
    }
}
